

const {writeFileSync,unlinkSync,readFileSync} = require("fs")
const {resolve} = require("path")
const {load} = require('js-yaml')
const metaType = require('./gen-mapping')

const genConfig = require('./table-template.js')
// const simpleConfig = require('./table-simple-config.js')
function readYaml(pth){
    try {
     const text =  readFileSync(pth,{encoding:'utf-8'})
     return load(text)
    } catch (error) {
     console.error(error)
     return undefined
    }
 }
 
 
function genCode(data){
    const c = String.fromCharCode(8205)
    const result  = JSON.stringify(data,(k,v)=>{
      
        if(typeof(v)==='function'){
            return c+v.toString()+c
        }
        return v
    },4)
    return 'export default '+ result
    .replace(/"\u200d(.*?)\u200d"/g,'$1')
    .replace(/\\"/g,'"')
    .replace(/\\'/g,"'")
}

// const configDir = resolve(__dirname,'../src/config')
const configDir = "E:\\code\\vue-demo\\src\\config"
// const configDir = resolve("D:\\codes\\nodejs\\vue-demo","src/config")


Object.keys(metaType).forEach(k=>{
    const simpleConfig = readYaml(resolve(__dirname,`types/${k}.yaml`))
    const targetPath = resolve(configDir,`${metaType[k]}.js`)
    const data = genConfig(simpleConfig)
    writeFileSync(targetPath,genCode(data),{encoding:"utf-8"})
})
// unlinkSync(targetPath)